﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HeresyManager;
using System.Collections.ObjectModel;

/// <summary>
/// Summary description for UserData
/// </summary>
[Serializable]
public class UserData
{
    private List<Character> characters;

    public UserData()
    {
        characters = new List<Character>();
    }

    public static ReadOnlyCollection<Character> Characters
    {
        get
        {
            ProfileCommon profile = HttpContext.Current.Profile as ProfileCommon;
            return profile.UserData.characters.AsReadOnly();
        }
    }

    public static Character CurrentCharacter
    {
        get { return HttpContext.Current.Session["CurrentCharacter"] as Character; }
    }
    public static int CurrentCharacterIndex
    {
        get { return (int)HttpContext.Current.Session["CurrentCharacterIndex"]; }
    }

    public static void Commit()
    {
        ProfileCommon profile = HttpContext.Current.Profile as ProfileCommon;
        var index = (int)HttpContext.Current.Session["CurrentCharacterIndex"];
        profile.UserData.characters[index] = CurrentCharacter;
        profile.Save();
    }

    public static void Roleback()
    {
        var profile = (HttpContext.Current.Profile as ProfileCommon).UserData;
        var index = (int)HttpContext.Current.Session["CurrentCharacterIndex"];
        BeginEdit(index);

    }

    public static void BeginEdit(int index)
    {
        if (index == -1)
        {
            HttpContext.Current.Session["CurrentCharacter"] = null;
            HttpContext.Current.Session["CurrentCharacterIndex"] = -1;
        }
        var profile = (HttpContext.Current.Profile as ProfileCommon).UserData;

        var c = profile.characters[index];

        var serelizer = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

        var mStream = new System.IO.MemoryStream();
        serelizer.Serialize(mStream, c);
        mStream.Position = 0;

        c = serelizer.Deserialize(mStream) as Character;

        HttpContext.Current.Session["CurrentCharacter"] = c;
        HttpContext.Current.Session["CurrentCharacterIndex"] = index;
    }

    public static void AddCharacter(HeresyManager.Character c)
    {
        ProfileCommon profile = HttpContext.Current.Profile as ProfileCommon;
        int index = profile.UserData.characters.BinarySearch(c, new CharComparar());

        if (index < 0)
            index = ~index;

        if (CurrentCharacter != null)
            if (index <= (int)HttpContext.Current.Session["CurrentCharacterIndex"])
                HttpContext.Current.Session["CurrentCharacterIndex"] = (int)HttpContext.Current.Session["CurrentCharacterIndex"] + 1;
        profile.UserData.characters.Insert(index, c);
        profile.Save();
    }

    public static void RemoveCharacter(int index)
    {
        ProfileCommon profile = HttpContext.Current.Profile as ProfileCommon;


        if (CurrentCharacter != null)
        {
            if (index <= (int)HttpContext.Current.Session["CurrentCharacterIndex"])
                if (index == (int)HttpContext.Current.Session["CurrentCharacterIndex"])
                    throw new Exception("Current Selected Character Can Not Be Deleted");
                else
                    HttpContext.Current.Session["CurrentCharacterIndex"] = (int)HttpContext.Current.Session["CurrentCharacterIndex"] - 1;
        }
        profile.UserData.characters.RemoveAt(index);
        profile.Save();    
    }




    private class CharComparar : IComparer<Character>
    {
        public int Compare(Character x, Character y)
        {
            return x.Name.CompareTo(y.Name);
        }
    }
}